package com.everbest.system.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.everbest.common.core.text.Convert;
import com.everbest.system.domain.SysLogininfor;
import com.everbest.system.domain.SysSMSVerify;
import com.everbest.system.mapper.SysLogininforMapper;
import com.everbest.system.mapper.SysSMSVerifyMapper;
import com.everbest.system.service.ISysLogininforService;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService {

    @Autowired
    private SysLogininforMapper logininforMapper;

    @Autowired
    private SysSMSVerifyMapper sysSMSVerifyMapper;

    /**
     * 新增系统登录日志
     * 
     * @param logininfor
     *            访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param logininfor
     *            访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
        return logininforMapper.selectLogininforList(logininfor);
    }

    /**
     * 批量删除系统登录日志
     * 
     * @param ids
     *            需要删除的数据
     * @return
     */
    @Override
    public int deleteLogininforByIds(String ids) {
        return logininforMapper.deleteLogininforByIds(Convert.toStrArray(ids));
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor() {
        logininforMapper.cleanLogininfor();
    }

    @Override
    public String updateVefiryCode(String phone, String verifyCode) {
        SysSMSVerify res = sysSMSVerifyMapper.getVerifyCodeByPhone(phone);
        Long currentDateTime = System.currentTimeMillis();
        if (res != null && currentDateTime - res.getUpdateTime().getTime() < 5 * 60 * 1000) {
            return "短信在有效期，无需重复获取";
        } else if (res != null) {
            Calendar now = Calendar.getInstance();
            now.set(Calendar.HOUR_OF_DAY, 0);
            now.set(Calendar.MINUTE, 0);
            now.set(Calendar.SECOND, 0);
            now.set(Calendar.MILLISECOND, 0);
            Date today = now.getTime();
            Date nowTime = res.getUpdateTime();
            if (nowTime.after(today)) {
                if (res.getUseTimesToday() >= 3) {
                    return "获取失败！同一手机号每天最多获取3次验证码！";
                }
                res.setUseTimesToday(res.getUseTimesToday() + 1);
            } else {
                res.setUseTimesToday(2);
            }
            res.setPhone(phone);
            res.setVerifyCode(verifyCode);
            sysSMSVerifyMapper.insertOrUpdateVerifyCode(res);
        } else {
            SysSMSVerify verifyBean = new SysSMSVerify();
            verifyBean.setPhone(phone);
            verifyBean.setVerifyCode(verifyCode);
            verifyBean.setUseTimesToday(1);
            sysSMSVerifyMapper.insertOrUpdateVerifyCode(verifyBean);
        }
        return null;
    }

    @Override
    public SysSMSVerify getVerifyCode(String phone) {
        SysSMSVerify res = sysSMSVerifyMapper.getVerifyCodeByPhone(phone);
        if (res == null)
            return null;
        Long currentDateTime = System.currentTimeMillis();
        if (currentDateTime - res.getUpdateTime().getTime() < 5 * 60 * 1000) {
            return res;
        }
        return null;
    }

}
